import { _decorator, Component, Node, Button, Label, ProgressBar, Animation } from 'cc';
import { eventCenter } from '../../Utils/JJWXR_EventCenter';
import { JJWXR_Events } from '../../Utils/JJWXR_Events';
import { ProjectEvent, ProjectEventManager } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
import Banner from 'db://assets/Scripts/Banner';
const { ccclass, property } = _decorator;

@ccclass('JJWXR_SucceedUI')
export class JJWXR_SucceedUI extends Component {
    private curLevel: number = 0;
    @property(Button)
    private btnComeBack: Button = null;
    @property(Button)
    private btnMoreReward: Button = null;
    @property(Button)
    private btnGetReward: Button = null;

    @property(Node)
    private progressBar: Node = null;
    @property(Label)
    private progressLabel: Label = null;

    // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    @property(Label)
    private spendTime: Label = null;
    @property(Label)
    private headShotRate: Label = null;
    @property(Label)
    private hitRate: Label = null;
    // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    // 添加进度
    @property(Node)
    private gun1: Node = null;
    @property(Node)
    private gun2: Node = null;

    @property(Node)
    private animNode: Node = null;

    private onceTouch: boolean = false;

    static headShot = 0;
    static hitShot = 0;

    onLoad() {
        this.btnComeBack.node.on(Button.EventType.CLICK, this.onComeBack, this);
        this.btnMoreReward.node.on(Button.EventType.CLICK, this.onMoreReward, this);
        this.btnGetReward.node.on(Button.EventType.CLICK, this.onGetReward, this);
    }

    start() {
        ProjectEventManager.emit(ProjectEvent.游戏结束);
        let currentLevel = (localStorage.getItem('currentLevel'));
        console.log('level:', currentLevel); // 获取当前关卡

        let data = localStorage.getItem('progress');
        let progress = { total: 3, current: 0, gunIndex: 1 };
        if(data != null && data != "") progress = JSON.parse(data);
        localStorage.setItem('progress', JSON.stringify(progress)); // 初始化进度

        if (progress.gunIndex === 1) {
            this.gun1.active = true;
            this.gun2.active = false;
        } else if (progress.gunIndex === 2) {
            this.gun1.active = false;
            this.gun2.active = true;
        }

        this.updateSucceedUI();
        eventCenter.on(JJWXR_Events.UPDATE_SUCCEED_UI, this.updateSucceedUI, this);
    }

    onDestroy() {
        eventCenter.off(JJWXR_Events.UPDATE_SUCCEED_UI, this.updateSucceedUI, this);
    }

    updateSucceedUI() {
        console.log('update succeed ui');
        this.addProgress();
        this.showSpendTime();
        this.showHeadShotRate();
        this.showHitRate();
    }

    // 点击返回按钮时触发
    onComeBack() {
        this.onceTouch = true;
        if (this.onceTouch) {
            this.onNextLevel();
            eventCenter.emit(JJWXR_Events.GET_SUCCEED_MONEY);   // 获取奖励  
        }
        eventCenter.emit(JJWXR_Events.RESTART_GAME); // 关卡切换
    }

    // 点击更多奖励按钮时触发
    onMoreReward() {
        let x = this;
        Banner.Instance.ShowVideoAd(() => {
            if (!x.onceTouch) {
                x.onNextLevel();
                // eventCenter.emit(JJWXR_Events.GET_MORE_REWARD_SCENE, 250);   // 更多奖励
                eventCenter.emit(JJWXR_Events.STOP_MOVE_BAR);   // 获取奖励
                x.animNode.getComponent(Animation).play(); // 播放动画
                x.onceTouch = true;
            }
            x.scheduleOnce(() => {
                eventCenter.emit(JJWXR_Events.RESTART_GAME); // 关卡切换
            }, 2.5);
        });
    }

    // 点击领取奖励按钮时触发
    onGetReward() {

        if (!this.onceTouch) {
            this.onNextLevel();
            eventCenter.emit(JJWXR_Events.GET_SUCCEED_MONEY);   // 获取奖励
            this.animNode.getComponent(Animation).play(); // 播放动画
            this.onceTouch = true;
        }
        this.scheduleOnce(() => {
            eventCenter.emit(JJWXR_Events.RESTART_GAME); // 关卡切换
        }, 2);
    }

    // 关卡切换
    onNextLevel() {
        let currentLevel = parseInt(localStorage.getItem('currentLevel'));

        let shotNumber = (localStorage.getItem('shotNumber'));
        // let hitShot = (localStorage.getItem('hitShot'));
        // let headShot = (localStorage.getItem('headShot'));

        JJWXR_SucceedUI.headShot = 0;
        JJWXR_SucceedUI.hitShot = 0;
        // 数据归零
        localStorage.setItem('shotNumber', shotNumber);
        // localStorage.setItem('hitShot', hitShot);
        // localStorage.setItem('headShot', headShot);

        // if (currentLevel < 6) {
        currentLevel++;
        localStorage.setItem('currentLevel', (currentLevel).toString());
        // let curlevel = JSON.parse(localStorage.getItem('currentLevel'));
        // console.log('level:', curlevel);
        // }
        // else {
        //     currentLevel = 1;
        //     localStorage.setItem('currentLevel', (currentLevel).toString());
        // }
    }

    // 进度条增加
    addProgress() {
        const progress = JSON.parse(localStorage.getItem('progress'));
        console.log(progress.total + " " + progress.current + " " + progress.gunIndex);
        let cur = progress.current;
        let total = progress.total;
        cur += 1;
        if (cur > total) {
            cur = 0;
        }
        this.progressBar.getComponent(ProgressBar).progress = cur / total;
        // console.log('progress:', cur + " " + total);
        this.progressLabel.string = cur + "/" + total;
        progress.current = cur;
        localStorage.setItem('progress', JSON.stringify(progress));

        if (progress.current >= progress.total && progress.gunIndex == 1) {
            console.log('奖励枪:');
            const gunReward = JSON.parse(localStorage.getItem('gun5'));
            gunReward.isBuy = true;
            localStorage.setItem('gun5', JSON.stringify(gunReward));

            eventCenter.emit(JJWXR_Events.UPDATE_BUTTON_STATE);   // 更多奖励
            const index = progress.gunIndex + 1;
            progress.gunIndex = index;
            localStorage.setItem('progress', JSON.stringify(progress));
        }
        if (progress.current >= progress.total && progress.gunIndex == 2) {
            const gunReward = JSON.parse(localStorage.getItem('gun6'));
            gunReward.isBuy = true;
            localStorage.setItem('gun6', JSON.stringify(gunReward));
            eventCenter.emit(JJWXR_Events.UPDATE_BUTTON_STATE);   // 更多奖励
        }

        // // 重置progress
        if (progress.current == progress.total) {
            progress.current = 0;
            progress.total = 3;
            localStorage.setItem('progress', JSON.stringify(progress));
        }
    }

    //  显示用时
    showSpendTime() {
        let spendTime = localStorage.getItem('spendTime');
        spendTime = Math.floor(parseInt(spendTime)).toString();
        this.spendTime.string = spendTime + "秒";
        console.log('spendTime:', spendTime);
    }

    // 显示爆头率
    showHeadShotRate() {
        // let headShot = localStorage.getItem('headShot');
        let shotNumber = localStorage.getItem('shotNumber');
        this.headShotRate.string = Math.floor((JJWXR_SucceedUI.headShot / parseInt(shotNumber)) * 100) + "%";
        console.log('headShot:', (JJWXR_SucceedUI.headShot / parseInt(shotNumber)));
    }

    // 显示命中率
    showHitRate() {
        // let hitRate = localStorage.getItem('hitShot');
        let shotNumber = localStorage.getItem('shotNumber');
        this.hitRate.string = Math.floor((JJWXR_SucceedUI.hitShot / parseInt(shotNumber)) * 100) + "%";
        console.log('hitRate:', (JJWXR_SucceedUI.hitShot / parseInt(shotNumber)));
    }
}